import 'package:flutter/material.dart';
import 'package:flutter_base_app/export.dart';
import 'package:flutter_base_app/page/view/unknown_page.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_live_data/live_data_export.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'config/base_framework.dart';
import 'config/app_colors.dart';
import 'config/i18n/i18n.dart';
import 'config/pigeons/pigeons_config.dart';
import 'config/theme.dart';
import 'constant/app_global.dart';
import 'router/routes_manager.dart';
import 'router/transition.dart' show ScaleFadeTransition;

void main() async {
  // 尝试请求高刷新率（需平台支持）
  WidgetsFlutterBinding.ensureInitialized();
  await AppUtils.setScreenPortrait();
  //工具初始化
  await BaseAppUtils.init();
  BaseFramework.init();
  //初始化路由
  AppRoutes.init();
  // await VkCmdManage.appStart();
  //信鸽原生交互
  PigeonsConfig.init();
  //第三方sdk初始化
  await sdkInit();
  runApp(const MyApp());
}

//第三方sdk初始化
Future<void> sdkInit() async {}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    LogUtils.i("MyApp build");
    AppGlobal.init(context);
    AppUtils.initWithContext(context);
    return MediaQuery(
      //禁止缩放字体大小
      data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1)),
      child: GetMaterialApp(
        getPages: AppRoutes.routes,
        defaultTransition: Transition.rightToLeft,
        initialRoute: RouterKey.welcome,
        navigatorKey: AppRoutes.navigatorKey,
        // defaultTransition: Transition.rightToLeftWithFade,
        customTransition: ScaleFadeTransition(),
        // transitionDuration: const Duration(milliseconds: 2000),
        navigatorObservers: [PageLifecycleObserver(), routeRecord],
        unknownRoute: GetPage(name: RouterKey.unknown, page: () => const UnknownPage()),
        // 国际化
        translations: I18nConfig.create(),
        // 将会按照此处指定的语言翻译
        // locale: const Locale('zh', 'CN'),
        locale: I18nConfig.locale,
        // 添加一个回调语言选项，以备上面指定的语言翻译不存在
        fallbackLocale: I18nConfig.localeEnUs,
        title: AppUtils.appName,
        theme: lightTheme,
        darkTheme: darkTheme,
        themeMode: ThemeUtils.themeMode,
        builder: FlutterSmartDialog.init(builder: (context, child) {
          return DefaultTextStyle(
            style: const TextStyle(fontSize: 14, color: ColorsApp.black),
            child: child!,
          );
        }),
      ),
    );
  }
}
